
#include "http_protocol_parser.h"
#include "string_util.h"

#include <cstdint>
#include <cstdio>
#include <gtest/gtest.h>
#include <vector>

TEST(HttpProtocolResponse, HeaderLine)
{
    std::string                line = "HTTP/1.1 200 OK\r\n";
    HttpResponseProtocolParser parser;
    int                        length = 0;
    int                        result = parser.ParseHttpResponseProtocol((const uint8_t *)line.c_str(), line.size(), length);
    EXPECT_EQ(result, HttpResponseProtocolParser::NEED_DATA);
    printf("line : %ld and use length : %d.\n", line.size(), length);
}

TEST(HttpProtocolResponse, HeaderLine206)
{
    std::string                line = "HTTP/1.1 206 OK Content\r\n";
    std::string c ="HTTP/1.1 206 Partial Content\r\nServer: JSP3/2.0.14\r\nDate: Sat, 19 Jul 2025 12:41:41 GMT\r\nContent-Type: text/javascript; charset=utf-8\r\nContent-Length: 0\r\n\r\n";
    line = c;
    HttpResponseProtocolParser parser;
    int                        length = 0;
    int                        result = parser.ParseHttpResponseProtocol((const uint8_t *)line.c_str(), line.size(), length);
    EXPECT_EQ(result, HttpResponseProtocolParser::FINISH);
    printf("line : %ld and use length : %d.\n", line.size(), length);
    printf("code:%s.\n", parser.Response()->GetCode().c_str());
}

TEST(HttpProtocolResponse, HeaderSign)
{
    std::string                sign = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: keep-alive\r\n\r\n";
    HttpResponseProtocolParser parser;
    int                        length = 0;
    int                        result = parser.ParseHttpResponseProtocol((const uint8_t *)sign.c_str(), sign.size(), length);
    EXPECT_EQ(result, HttpResponseProtocolParser::FINISH);
    printf("line : %ld and use length : %d.\n", sign.size(), length);
}

TEST(HttpProtocolResponse, Body)
{
    std::string body = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 10\r\n\r\n0123456789";
    HttpResponseProtocolParser parser;
    int                        length = 0;
    int                        result = parser.ParseHttpResponseProtocol((const uint8_t *)body.c_str(), body.size(), length);
    EXPECT_EQ(result, HttpResponseProtocolParser::FINISH);
    printf("body size : %ld and use length : %d and res body size : %ld.\n", body.size(), length,
           parser.Response()->Body().size());
}

TEST(HttpProtocolResponse, MultiBody)
{
    std::string body = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 16\r\n\r\n0123456789";
    HttpResponseProtocolParser parser;
    int                        length = 0;
    int                        result = parser.ParseHttpResponseProtocol((const uint8_t *)body.c_str(), body.size(), length);
    EXPECT_EQ(result, HttpResponseProtocolParser::NEED_DATA);
    printf("body size : %ld and use length : %d and res body size : %ld.\n", body.size(), length,
           parser.Response()->Body().size());
    body   = "abcdef";
    result = parser.ParseHttpResponseProtocol((const uint8_t *)body.c_str(), body.size(), length);
    EXPECT_EQ(result, HttpResponseProtocolParser::FINISH);
    printf("body size : %ld and use length : %d and res body size : %ld.\n", body.size(), length,
           parser.Response()->Body().size());
    auto res_body = parser.Response()->Body();
    for (auto & c : res_body)
    {
        printf("%c", c);
    }
    printf(".\n");
}

TEST(BaiduResponse, res)
{
    std::string c =
        "485454502f312e3120323030204f4b0d0a446174653a205361742c203237204a756c20323032342030313a32313a303220474d540d0a5365727665723a204170616368650d0a4c6173742d4d6f6469666965643a205475652c203132204a616e20323031302031333a34383a303020474d540d0a455461673a202235312d34376366376536656538343030220d0a4163636570742d52616e6765733a2062797465730d0a436f6e74656e742d4c656e6774683a2038310d0a43616368652d436f6e74726f6c3a206d61782d6167653d38363430300d0a457870697265733a2053756e2c203238204a756c20323032342030313a32313a303220474d540d0a436f6e6e656374696f6e3a204b6565702d416c6976650d0a436f6e74656e742d547970653a20746578742f68746d6c0d0a0d0a3c68746d6c3e0a3c6d65746120687474702d65717569763d22726566726573682220636f6e74656e743d22303b75726c3d687474703a2f2f7777772e62616964752e636f6d2f223e0a3c2f68746d6c3e0a";
    std::vector<uint8_t> data;
    StringUtil::HexStrToCharacter(c, data);
    int                        length = 0;
    HttpResponseProtocolParser parser;
    int                        result = parser.ParseHttpResponseProtocol(data.data(), data.size(), length);
    EXPECT_EQ(result, HttpResponseProtocolParser::FINISH);
    printf("data size : %ld and use length : %d.\n", data.size(), length);
}

int main(int argc, char * argv[])
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
